package com.doctorliang.cheng.shopping.type.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.doctorliang.cheng.shopping.R;

/**
 * Created by YBH on 2017/12/14.
 */

public class TypeLeftAdapter extends BaseAdapter {

    private String[] titles = new String[]{"小裙子", "上衣", "下装", "外套", "配件", "包包", "装扮", "居家宅品", "办公文具", "数码周边", "游戏专区"};
    private  Context mContext;
    private int mSelect = 0;//选中项

    public TypeLeftAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public Object getItem(int position) {
        return titles[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_type, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_name = convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //设置标题
        viewHolder.tv_name.setText(titles[position]);
        if (mSelect == position) {
            convertView.setBackgroundResource(R.drawable.type_item_background_selector);//更换背景
            viewHolder.tv_name.setTextColor(Color.parseColor("#fd3f8f"));//更换字体颜色
        } else {
            convertView.setBackgroundResource(R.drawable.bg2);  //其他项背景
            viewHolder.tv_name.setTextColor(Color.parseColor("#323437"));
        }
        return convertView;
    }

    static class ViewHolder {
        private TextView tv_name;
    }


    /**
     * 刷新数据
     * @param position
     */
    public void changeSelect(int position){
        if (position!=mSelect) {
            mSelect=position;
            notifyDataSetChanged();
        }
    }
}
